package com.nervenets.template.hibernate.dao;

import com.nervenets.general.hibernate.dao.BaseDao;
import com.nervenets.template.enumeration.OperateType;
import com.nervenets.template.enumeration.WealthType;
import com.nervenets.template.hibernate.domain.user.WealthFlow;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface WealthFlowDao extends BaseDao<WealthFlow> {
    WealthFlow findTop1ByUserIdAndWealthTypeOrderByIdDesc(long userId, WealthType wealthType);

    @Query("select operateType as operateType, coalesce(sum(offset),0) as total from WealthFlow where userId = :userId and wealthType = :wealthType group by operateType")
    List<Map> findWealthMapByUserId(@Param("userId") long userId, @Param("wealthType") WealthType wealthType);

    @Query("select new java.math.BigDecimal(coalesce(sum(offset),0)) from WealthFlow where wealthType = :wealthType and operateType in (:operateTypes) and createTime between :startTime and :endTime")
    BigDecimal sumOf(WealthType wealthType, List<OperateType> operateTypes, long startTime, long endTime);
}
